/**********************************************************************************************/
$(function () {
	SetButtonsEffect();

	//
	$('.flowplayer').each(function () {
		var player = $(this);
		// 计算宽高
		var ratio = player.attr('ratio');
		ratio = ratio.trim();
		if (ratio.legnth == 0)
			ratio = parseFloat(640/480);
		else
			ratio = parseFloat(eval('(' + ratio + ')'));
		player.css('width', '100%');
		var w = player.width();
		var h = parseInt(w / ratio);
		console.log("w = " + w);
		console.log("ratio = " + ratio);
		player.css('height', h);
		// 获取背景
		var bg = player.attr('bgurl');
		bg = bg.trim();
		if (bg.length > 0) {
			player.css('background-image', 'url(' + bg + ')');
		}
		player.flowplayer({
			tooltip: false,
			fullscreen: true
		});
		// 播放器样式控制
		$('.fp-embed').hide();
	});
});
/**********************************************************************************************/
/**********************************************************************************************/
// RPC handler
var RPC_URL = "ajax_handler.php";

/*
 * 公共函数
 */
// 字符串修剪
String.prototype.trim = function () {
	return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
};

//辅助函数，验证传入函数的字符串是否全为数字
function CheckDigit(str) {
	var letters = "0123456789";
	for (var i = 0; i < str.length; i++) {
		if (letters.indexOf(str[i]) == -1)
			return false;
	}
	return true;
}

/*
 * 模块函数
 */

// 打开抽屉
function popDrawerMenu(btn) {
	$(btn).attr("src", "img/menu-down.png");
	$("#mask").css({
		"position" : "fixed",
		"top" : "0px",
		"right" : "0px",
		"width" : $(window).width() + "px",
		"height" : $(window).height() + "px",
		"z-index" : 99998
	}).show();
	var dw = $(window).width();
	var w = parseInt(dw * 0.7);
	var left = dw - w;
	$("#drawer").css({
		"position" : "fixed",
		"top" : "0px",
		"left" : dw + "px",
		"width" : w + "px",
		"height" : $(window).height() + "px",
		"z-index" : 99999
	}).show().animate({
		"left" : left + "px"
	}, "fast"); //.show();
	setTimeout(function () {
		$(btn).attr("src", "img/menu.png");
	}, 500);
	var collapse = function () {
		$("#mask").hide();
		$("#drawer").hide();
	};
	$("#mask").mousedown(collapse).mousemove(collapse);
}

// toast 效果实现
/**
 * 模仿android里面的Toast效果，主要是用于在不打断程序正常执行的情况下显示提示数据
 * @param config
 * @return
 */
var Toast = function (config) {
	this.context = config.context == null ? $('body') : config.context; //上下文
	this.message = config.message; //显示内容
	this.time = config.time == null ? 3000 : config.time; //持续时间
	this.left = config.left; //距容器左边的距离
	this.top = config.top; //距容器上方的距离
	this.init();
};

// 设置对象屏幕居中
function centralizeObject(obj) {
	var wnd = $(window),
	doc = $(document);
	var left = doc.scrollLeft() || document.documentElement.scrollLeft;
	var top = doc.scrollTop() || document.documentElement.scrollTop;
	var ow = obj.width(),
	oh = obj.height();
	left += parseInt((wnd.width() - ow) / 2);
	top += parseInt((wnd.height() - oh) / 3);
	obj.css("position", "absolute");
	obj.css("top", top);
	obj.css("left", left);
}

//toast消息
function MakeToast(str) {
	$('#_global_toast').remove();
	var dlg = $("<table id='_global_toast'><tr><td></td></tr></table>").appendTo("body");
	dlg.hide();
	dlg.css({
		"background" : "url(img/opacity_mask.png)",
		"z-index" : "100001",
		"color" : "white",
		"font-weight" : "bold",
		"width" : "auto"
	});
	dlg.find("td").eq(0).css("padding", "20px").html(str);
	dlg.corner("3px");
	centralizeObject(dlg);
	dlg.fadeIn();
	setTimeout(function () {
		dlg.remove();
	}, 3000);
}

// 订单数据校验
function CheckOrderData() {
	var valid = false;

	// 校验用户名
	var username = $("#order-form input[name='realName']").eq(0).val();
	username = username.trim();
	if (username.length == 0) {
		MakeToast("请填写您的名字");
		return false;
	}

	// 校验用户联系方式
	valid = false;
	do {
		var contact = $("#order-form input[name='userTel']").eq(0).val();
		contact = contact.trim();
		if (contact.length < 7)
			break;
		if (!CheckDigit(contact))
			break;
		valid = true;
	} while (false);
	if (!valid) {
		MakeToast("请填写您的联系方式");
		return false;
	}

	// 校验人数
	valid = false;
	do {
		var num = $("#order-form input[name='personCount']").eq(0).val();
		num = num.trim();
		if (num.length == 0)
			break;
		if (!CheckDigit(num)) {
			MakeToast("就餐人数填写不正确，只能是数字");
			return false;
		}
		if (parseInt(num) == 0)
			break;
		valid = true;
	} while (false);
	if (!valid) {
		MakeToast("请填写就餐人数");
		return false;
	}

	// 校验日期
	var date = $("#order-form input[name='serviceDate']").eq(0).val();
	date = date.trim();
	if (date.length == 0) {
		MakeToast("请选择就餐时间");
		return false;
	}
	date = $("#order-form input[name='serviceTime']").eq(0).val();
	date = date.trim();
	if (date.length == 0) {
		MakeToast("请选择就餐时间");
		return false;
	}

	// 校验地点
	var addr_idx = $("#order-form input[name='address']:checked").eq(0).val();
	if (addr_idx == 1) {
		var addr = $("#order-form input[name='custom_address']").eq(0).val();
		addr = addr.trim();
		if (addr.length == 0) {
			MakeToast("请填写您的就餐地址");
			return false;
		}
	}

	return true;
}

// 点赞
function SetFocus(cook_id, user_id, obj) {
	// get initial status
	var zan = parseInt($(obj).attr('zan'));
	var num = parseInt($(obj).find('font').eq(0).text());
	var img = $(obj).find('img').eq(0);

	// 数字更新
	if (zan == 1) {
		// 赞过了，减掉数字
		num -= 1;
		if (num < 0) {
			num = 0;
		}
		img.attr("src", "img/xin-k.png");
		$(obj).attr('zan', '0');
		$(obj).find("font").eq(0).html(num + "");
	} else {
		// 没有赞过，加1
		num += 1;
		img.attr("src", "img/xin.png");
		$(obj).attr('zan', '1');
		$(obj).find("font").eq(0).html(num + "");
	}

	// 更新服务器数据
	$.post(RPC_URL, {
		"cook_id" : cook_id,
		"user_id" : user_id,
		"zan" : zan,
		"ajax_action" : "SetFocus"
	}, function (str) {
		try {
			var json = eval("(" + str + ")");
			if (json.errcode != 0) {
				//MakeToast(json.errmsg);
			} else {}
		} catch (e) {}
	});
}

/*
 * 厨师界面点赞
 */
var zanb = {
	"status" : "-1",
	"num" : "-1"
};
function SetFocus1(cook_id, user_id, obj) {
	if (zanb["status"] == -1) {
		zanb["status"] = parseInt($(obj).attr("zan"));
		zanb["num"] = parseInt($(obj).find("span").html());
	}
	var img = $(obj).find("img");

	var zan = zanb["status"];
	var num = zanb["num"];

	// 数字更新
	if (zan == 1) {
		// 赞过了，减掉数字
		num -= 1;
		if (num < 0) {
			num = 0;
		}
		img.attr("src", "img/xin.white.png");
		zanb["status"] = 0;
		zanb["num"] = num;
		$(obj).find("span").html(num + "");
	} else {
		// 没有赞过，加1
		num += 1;
		img.attr("src", "img/xin.red.png");
		zanb["status"] = 1;
		zanb["num"] = num;
		$(obj).find("span").html(num + "");
	}

	// 更新服务器数据
	$.post(RPC_URL, {
		"cook_id" : cook_id,
		"user_id" : user_id,
		"zan" : zan,
		"ajax_action" : "SetFocus"
	}, function (str) {
		try {
			var json = eval("(" + str + ")");
			if (json.errcode != 0) {
				//MakeToast(json.errmsg);
			} else {}
		} catch (e) {}
	});
}

//按钮效果
function SetButtonsEffect() {
	$(".round-btn-green,.round-btn-lightred,.round-btn-green-small").each(function () {
		$(this).corner('3px');
	});

	$('.round-btn-green,.round-btn-gray').corner('3px');
	$('.round-btn-green-right').corner('right 3px');
	$('.round-btn-gray-left').corner('left 3px');

	// 点击态效果
	$(".round-btn-green,.round-btn-green-small,.round-btn-green-right").click(function () {
		var item = $(this);
		item.addClass('bgcolor-green');
		setTimeout(function () {
			item.removeClass('bgcolor-green');
		}, 100);
	});
	$(".round-btn-gray,.round-btn-gray-left").click(function () {
		var item = $(this);
		item.addClass('bgcolor-silver');
		setTimeout(function () {
			item.removeClass('bgcolor-silver');
		}, 100);
	});
}

// 不加蒙版的loading
var __LOADING_IMG_WITHOUT_MASK = false;
function PopLoadingWithoutMask() {
	try {
		__LOADING_IMG_WITHOUT_MASK.remove();
	} catch (e) {}
	__LOADING_IMG_WITHOUT_MASK = $("<img src='img/loading-white.gif?t=" + Math.random() + "' />").appendTo("body");

	var img = new Image();
	img.src = __LOADING_IMG_WITHOUT_MASK.attr("src");
	var w = img.width;
	var h = img.height;
	var left = parseInt(($(window).width() - w) / 2);
	__LOADING_IMG_WITHOUT_MASK.css({
		"position" : "absolute",
		"left" : left,
		"top" : parseInt(($(window).height() - h) / 2)
	});
}

// 跳转页面
function changeLocation(url) {
	window.location.href = url;
}
